# 2.2、物品相关 API

from fastapi import FastAPI,APIRouter

from app.schemas.items import ItemCreate, ItemResponse
from app.models.item import Item


router = APIRouter(prefix="/items", tags=["Items"])


@router.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "query": q}


@router.post("/", response_model=ItemResponse)
async def create_item(item: ItemCreate):
    """创建商品接口"""
    # 将 Pydantic 模型转换为数据库模型
    db_item = Item(**item.dict())
    # 模拟保存到数据库
    await db_item.save()
    return db_item
